package com.xying.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.xying.common.ServerResponse;
import com.xying.pojo.Usr;
import com.xying.service.VoiceService;
import com.xying.util.JsonUtil;
import com.xying.util.MD5Util;
import com.xying.util.PropertiesUtil;
import com.xying.util.ValidateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * 声纹处理相关，用于用户新建声纹等操作
 * Created by Hisen on 2021/4/25.
 */
@Controller
@RequestMapping("/voice")
@Slf4j
public class VoiceController {

    @Autowired
    private VoiceService voiceService;



    /**
     * 注册声纹
     * @param file
     * @param idNo
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "registerVoice.do", method = RequestMethod.POST)
    public ServerResponse registerVoice(@RequestParam(value = "upload_file", required = false) MultipartFile file, String idNo, HttpServletRequest request) {
        ServerResponse<Usr> serverResponse = ValidateUtil.validateNormalLogin(request);
        if (!serverResponse.isSuccess()) {
            return serverResponse;
        }
        Usr usr = serverResponse.getData();
        if (!usr.getIdNo().equals(idNo)) {
            return ServerResponse.createByErrorMessage("越权操作，用户信息不一致");
        }

        if (file == null){
            return ServerResponse.createByErrorMessage("文件错误");
        }

        return voiceService.registerVoice(file, idNo, usr);
    }

    /**
     * 删除声纹
     * @param idNo
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "removeVoice.do")
    public ServerResponse removeVoice(String idNo, HttpServletRequest request) {
        ServerResponse<Usr> serverResponse = ValidateUtil.validateNormalLogin(request);
        if (!serverResponse.isSuccess()) {
            return serverResponse;
        }
        Usr usr = serverResponse.getData();
        if (!usr.getIdNo().equals(idNo)) {
            return ServerResponse.createByErrorMessage("越权操作，用户信息不一致");
        }

        return voiceService.removeVoice(idNo, usr.getUserName());
    }

    /**
     * 删除声纹
     * @param idNo
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectVoice.do")
    public ServerResponse selectVoice(String idNo, HttpServletRequest request) {
        ServerResponse<Usr> serverResponse = ValidateUtil.validate(idNo);
        if (!serverResponse.isSuccess()) {
            return serverResponse;
        }

        return voiceService.selectVoice(idNo);
    }

    /**
     * 客户端获取全部声纹
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAllVoicePrint.do", method = RequestMethod.POST)
    public ServerResponse getAllVoicePrint(String jsonParam){
        ServerResponse serverResponse = ValidateUtil.validate(jsonParam);
        if (!serverResponse.isSuccess()){
            return serverResponse;
        }

        Map<String, String> requestMap = JsonUtil.stringToObj(jsonParam, Map.class);
        String username = requestMap.get("username");
        String password = requestMap.get("password");

        String clientUsername = PropertiesUtil.getProperty("client.username");
        String clientPassword = MD5Util.MD5EncodeUTF8(PropertiesUtil.getProperty("client.password"));

        if (!clientUsername.equals(username) || !clientPassword.equals(password)){
            return ServerResponse.createByErrorMessage("校验不通过");
        }

        return voiceService.getAllVoicePrint();
    }

}
